﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7Contrib.Collections;
using CDFWPUG.Model;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace CDFWPUG.Helpers
{
    public class LinksHelper
    {
        public static ObservableCollection<Link> ParseXML(String xml)
        {
            XElement xmlApps = XElement.Parse(xml);

            var items = from item in xmlApps.Descendants("link")
                        select new Link
                        {
                            Name = item.Element("name").Value,
                            Description = item.Element("description").Value,
                            Url = item.Element("url").Value,
                        };

            return new ObservableCollection<Link>(items);


        }


        public static ObservableCollection<Link> GetLinksFromIsolatedStorage()
        {
            string xml = Helpers.IsolatedStorageHelper.LoadFile("Links.xml");
            var results = ParseXML(xml);
            return results;
        }


        internal static ObservableCollection<Link> Test_GetLinks()
        {
            var resource = System.Windows.Application.GetResourceStream(new Uri("Data/Meetings.xml", UriKind.Relative));
            StreamReader streamReader = new StreamReader(resource.Stream);
            string xml = streamReader.ReadToEnd();
            streamReader.Close();
            var results = ParseXML(xml);
            return results;
        }
    }
}
